/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package programs;

import columnar.Columnarfile;
import diskmgr.PCounter;
import global.AttrType;
import global.TupleOrder;
import heap.Tuple;
import iterator.ColumnarSort;
import iterator.Sort;

/**
 *
 * @author SHIVANI
 */
public class ColumnarSortTest {
/*
    private static short REC_LEN_STRING = 160;
    private static short REC_LEN_INT = 32;
    private static short SORT_LEN_STRING = 32;
    private static short SORT_LEN_INT = 4;
*/
	
    public static Columnarfile cf = null;
    public static String columnDBName = "";
    public static String columnarFileName = "";
    public static int sortColumn = 0;
    public static int sortOrder = 0;
    public static int mSize = 0;

    public static void main (String [] args) throws Exception
    {
    	
        args = new String[5];
        args[0]= "ColumnDB2";
        args[1]= "ColumnarFile1";    //Name of the ColumnarFileName
        args[2]= "2";              //Name of the column to sort Columnarfile
        args[3]= "1";               //Tuple Sort order 0 = Ascending and 1 = decending
        args[4]= "100";              //No. of pages
        

        if (args.length != 5)
        {
            System.out.println("The command line invocation must be as follows: \n\t"
                                            + "COLUMNARDB COLUMNARFILE SortFieldNum SortOrderNum NumBuffPages");
            Runtime.getRuntime().exit(1);
        }
        
        columnDBName =		args[0];
        columnarFileName = 	args[1];
        sortColumn = 		Integer.parseInt(args[2]);
        sortOrder = 		Integer.parseInt(args[3]);
        mSize = 			Integer.parseInt(args[4]);
		
		
        // read an existing DB into BM
        //!!!!! NOTICE: NumBuff pages must be > than mSize !!!!!!!
        // otherwise there are no pages left to use for sorting algorithm
        global.SystemDefs sysdef = new global.SystemDefs(columnDBName, 0, 300, "Clock");

        // try read a columnarfiles form DB
        try {
                cf = new Columnarfile(columnarFileName);
        } catch (Exception ex) {
                System.err.println("Unable to read a columnar files from DB\n\t check if they have been created.\n");
                Runtime.getRuntime().exit(1);
        }

        ColumnarSort cSort = new ColumnarSort(
        		cf.getAttrTypes(),
        		(short)cf.NumberOfColumns(),
        		cf.getSizesOfFileds(),
        		columnarFileName,
        		sortColumn,
        		new TupleOrder(sortOrder),
        		cf.getSizesOfFileds()[sortColumn - 1],
        		mSize);
       
        int count = 0;
        try{
            Tuple tuple = new Tuple();
            while ((tuple = cSort.get_next()) != null) {
                tuple.print(cf.getAttrTypes());
                count++;
            }
        }
        catch (Exception ex) {
        	System.err.println(ex.toString());
        } 
        finally {
        	System.out.println("Records sorted in columnar file:" + count);
        	cSort.close();
        }
        sysdef.JavabaseBM.flushAllPages();
        // print number of processed pages
        PCounter.print();
    }
}